package com.finals.media.service;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;

import android.content.Context;
import android.os.Environment;

public class DownloadThread extends Thread {

	Context context;

	MusicePlayer musicePlayer;

	HttpURLConnection mConnection;

	String url;

	int cacheSize = 0;

	int CacheTotalSize = 0;

	public DownloadThread(Context context, MusicePlayer musicePlayer, String url) {
		this.context = context;
		this.musicePlayer = musicePlayer;
		this.url = url;
	}

	@Override
	public void run() {
		cacheSize = 0;
		URL mUrl = null;
		try {
			mUrl = new URL(url);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (mUrl != null) {
			try {
				mConnection = (HttpURLConnection) mUrl.openConnection();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		if (mConnection != null) {
			mConnection.setReadTimeout(5 * 1000);
			mConnection.setConnectTimeout(5 * 1000);
		}

		InputStream inputStream = null;

		RandomAccessFile fileOutputStream = null;
		File cacheFile = getCacheFile(context);
		if (cacheFile.exists()) {
			cacheFile.delete();
		}
		boolean isPrepar = false;

		try {
			mConnection.setRequestProperty("Accept-Encoding", "identity");
			mConnection.connect();
			inputStream = mConnection.getInputStream();
			CacheTotalSize = mConnection.getContentLength();

			fileOutputStream = new RandomAccessFile(cacheFile, "rw");
			fileOutputStream.setLength(CacheTotalSize);
			fileOutputStream.seek(0);

			byte[] buffer = new byte[49600];
			int len = 0;
			while ((len = inputStream.read(buffer)) != -1) {
				fileOutputStream.write(buffer, 0, len);
				cacheSize += len;
				if (cacheSize > 1024 * 50) {
					if (!isPrepar) {
						isPrepar = musicePlayer.Prepare(cacheFile, CacheTotalSize);
						if (isPrepar) {
							musicePlayer.Play();
						}
					}
				}
			}
			fileOutputStream.close();
			inputStream.close();

			fileOutputStream = null;
			inputStream = null;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (fileOutputStream != null) {
				try {
					fileOutputStream.close();
				} catch (Exception e2) {
					e2.printStackTrace();
				}
			}
			if (inputStream != null) {
				try {
					inputStream.close();
				} catch (Exception e2) {
					e2.printStackTrace();
				}
			}
		}
		CloseConnection();
	}

	public float getCachePercent() {
		if (CacheTotalSize == 0) {
			return 0;
		}
		float percent = (float) cacheSize / (float) CacheTotalSize;
		return percent;
	}

	private void CloseConnection() {
		if (mConnection != null) {
			try {
				mConnection.disconnect();
			} catch (Exception e) {
				e.printStackTrace();
			}
			mConnection = null;
		}
	}

	public File getCacheFile(Context context) {
		File resultFile = null;
		File rooFile = context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
		if (rooFile != null && rooFile.exists()) {
			if (!rooFile.canWrite()) {
				rooFile = null;
			}
		}
		if (rooFile == null) {
			rooFile = context.getFilesDir();
		}
		resultFile = new File(rooFile, "tmp.mp3");
		return resultFile;
	}

	public void StopDownload() {
		CloseConnection();
	}
}
